<?php
// Tim'Roster
// Copyright (c) 2011 Nicolas Nallet <aspgic2@gmail.com>
//
// This file is part of Tim'Roster.
//
// Tim'Roster is free software; you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Tim'Roster is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

session_start();

$xml_config = simplexml_load_file('config.xml');

include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/authentification/User.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/technical_class/language/language.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/class/Item_Base.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/render/render_guild_hall_room.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/render/render_menu.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/render/render_item.php');
include_once($_SERVER["DOCUMENT_ROOT"].$xml_config->roster_root_directory.'/render/render_header_footer.php');


if (!User::is_user_logged()){
    User::store_url_referer();
    header('Location: login.php');
}
else {
    if (!User::is_user_authorize(User::USER_GROUP_ID)){
        header('Location: unauthorized_access.php');
    }
}

if ((isset($_GET['type'])) || (isset($_GET['family'])) || (isset($_GET['code']))
        || ((isset($_GET['qlt_signe'])) && (isset($_GET['qlt'])))){



    $type = null;
    if ((isset($_GET['type'])) && (is_numeric($_GET['type']))) $type = $_GET['type'];



    $family = null;
    if ((isset($_GET['family'])) && (is_numeric($_GET['family']))) $family = $_GET['family'];

    $code = null;
    if ((isset($_GET['code'])) && (strlen($_GET['code']) < 10) && ($_GET['code'] != '')) $code = $_GET['code'];

    $quality = null;
    if ((isset($_GET['qlt'])) && is_numeric($_GET['qlt'])) $quality = $_GET['qlt'];

    $quality_com_signe = null;
    if (isset($_GET['qlt_signe'])){
        if (   ($_GET['qlt_signe'] == '=') || ($_GET['qlt_signe'] == '>')
            || ($_GET['qlt_signe'] == '<') || ($_GET['qlt_signe'] == '>=')
            || ($_GET['qlt_signe'] == '<=')){
            $quality_com_signe = $_GET['qlt_signe'];
        }
    }

    if ((isset($type)) || (isset($family)) || (isset($code))
        || ((isset($quality)) && (isset($quality_com_signe)))) {

        $item_array = Item_Base::search_item($type, $family, $code, $quality, $quality_com_signe);
    }
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="external_libraries/jquery-ui/js/jquery-1.4.4.min.js"></script>
        <script type="text/javascript" src="external_libraries/jquery-ui/js/jquery-ui-1.8.7.custom.min.js"></script>
        <script type="text/javascript" src="external_libraries/tooltip/tooltip.js"></script>
        <script src="external_libraries/msdropdown/js/jquery.dd.js" type="text/javascript"></script>


        <script type="text/javascript" src="js/menudialog.js"></script>
        <script type="text/javascript" src="js/toggle.js"></script>
        <script type="text/javascript" src="js/msdropdown.js"></script>
        <link rel="shortcut icon" type="images/x-icon" href="img/quest_jeton.png" />

        <style>
            @import url(css/base.css);
            @import url(external_libraries/jquery-ui/css/smoothness/jquery-ui-1.8.6.custom.css);
            @import url(external_libraries/msdropdown/dd.css);
        </style>

        <script type="text/javascript">
            //generate link for search button
            function search(){
                var type = document.getElementById('type_dd').options[document.getElementById('type_dd').selectedIndex].value;
                var family = document.getElementById('fam_dd').options[document.getElementById('fam_dd').selectedIndex].value;
                var code = document.getElementById('code_dd').options[document.getElementById('code_dd').selectedIndex].value;
                var quality = document.getElementById('qlt_dd').options[document.getElementById('qlt_dd').selectedIndex].value;
                var quality_signe = document.getElementById('qlt_signe_dd').options[document.getElementById('qlt_signe_dd').selectedIndex].value;

                self.location.href='item_search.php?type=' + type + '&family=' + family + '&code=' + code + '&qlt=' + quality + '&qlt_signe=' + quality_signe;
            }

            //Ajax function
            function refreshDropDown (f, list1, list2, param_name) {

                var l1    = f.elements[list1];
                var l2    = f.elements[list2];
                var index = l1.selectedIndex;

                resetDropDown(list1);

                if(index < 0)
                    l2.options.length = 0;
                else {
                    var xhr_object = null;

                    if(window.XMLHttpRequest) // Firefox
                        xhr_object = new XMLHttpRequest();
                    else if(window.ActiveXObject) // Internet Explorer
                        xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
                    else { // XMLHttpRequest not supported
                        alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
                        return;
                    }

                    xhr_object.open("POST", "search_ajax.php", true);

                    xhr_object.onreadystatechange = function() {
                        if(xhr_object.readyState == 4){
//                            alert(xhr_object.responseText);
                            eval(xhr_object.responseText);
                            $("body select ").msDropDown();
                        }
                    }

                    xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    var data = param_name+"="+escape(l1.options[index].value)+"&form="+f.name+"&select="+list2;
                    xhr_object.send(data);
                }
            }

            function resetDropDown(list){
                //reset code_dd after
                if (list == 'type_dd'){
                    //dirty but efficient (TODO : improve the code)
                    var c = document.getElementById("code_dd");
                    c.options.length = 0;
                    c.options[0] = new Option("All Code", "");
                }
            }

        </script>

    </head>
    <body>
        <center>
            <?php
                echo render_page_header().'</br>';
            ?>
            <table>
                <tr>
                    <td valign="top">
                        <?php
                            echo include('menu.php');
                        ?>
                    </td>
                    <td valign="top"  width="600">
                        <fieldset>
                            <h1 class="title">
                                <?php echo Language::get_instance()->get_label('lb_item_search'); ?>
                            </h1>
                            <form name="search_form" id="search_form">
                                <?php
                                    echo generate_item_type_family_and_code_dropdown();
                                    echo '<br/><br/><br/>'.generate_comparaison_signe_dropdown('qlt_signe_dd').generate_item_quality_dropdown();
                                ?>
                                <input type="button" value="Ok" onclick="search();" />

                                <br />
                                <br />
                                <?php
                                    if ((isset($type)) || (isset($family)) || (isset($code))
                                            || ((isset($quality)) && (isset($quality_com_signe)))){

                                         echo generate_search_resume($type, $family, $code, $quality, $quality_com_signe);
                                    }

                                    if (isset($item_array)){
                                        echo render_item_list_sortby_class($item_array);
                                    }
                                ?>
                            </form>
                        </fieldset>
                    </td>
                </tr>
            </table>
            <?php
                echo render_page_footer();
            ?>
        </center>
    </body>
</html>